﻿@model BBICMS.Model.ViewModel.Back.LinkListVM
@using BBICMS.Infrastructure;
@using PagedList;
@using PagedList.Mvc;
@{
    ViewBag.Title = "友情链接";
    ViewBag.PageTitle = ViewBag.Title + "_组件";
}



<div class="main-content">

    <div class="panel panel-default">
        <header class="panel-heading">
            <span class="glyphicon glyphicon-file"></span> @ViewBag.Title
        </header>

        <section class="panel-body">
            <div class="pull-right">
                <a class="btn btn-primary" href="@Url.Action("Add")"><i class="glyphicon glyphicon-plus"></i> 添加链接</a>
            </div>
            <div class="form-inline">
                @Html.DropDownList("ddlCategories", Model.Categories, new { @class = "form-control" })
            </div>
        </section>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>

                        <th>
                            链接
                        </th>
                        <th>
                            链接
                        </th>
                        <th>
                            所属分类
                        </th>
                        <th>
                            <span class="glyphicon glyphicon-time"></span> 创建日期
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var link in Model.OnePageOfLinks)
                    {
                        <tr class="first">

                            <td>
                                @link.Title
                            </td>
                            <td>
                                @link.WebLink
                            </td>
                            <td>
                                @link.CategoryTitle
                            </td>
                            <td>@link.AddedDate.ToShortDateString()</td>
                            <td>
                                <a href="@Url.Action("Edit", new { Id = link.Id })" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i></a>
                                <a href="@Url.Action("Delete", new { Id = link.Id })" class="btn btn-xs btn-danger delete-link"><i class="glyphicon glyphicon-trash"></i></a>
                            </td>
                        </tr>
                    }
                </tbody>

            </table>



        </div>


        <footer class="panel-footer">
            <ul class="pagination">
                @Html.PagedListPager(Model.OnePageOfLinks, page => Url.Action("Index", new { page = page, categoryId = Model.CategoryId }))
            </ul>
        </footer>


    </div>
</div>
<!-- end main container -->
@section footer {

    <script>

        $(function () {
            //当前链接;
            $('.mainmenu>li:nth-of-type(3)').addClass('nav-open').find('.submenu li:nth-of-type(3) a').addClass('active');

            //分类过滤链接
            $("#ddlCategories").change(function () {
                var cateId = $(this).val();
                location.href = "/bbi-Admin/Link/Index?categoryId=" + cateId;
            });

            //删除数据
            $('.delete-link').click(function (e) {
                var url = $(this).attr('href');
                var that = $(this);
               // console.log(url);
                /*DELETE*/
                $.ajax({
                    url: url,
                    dataType: "json",
                    type: "DELETE",
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({}),
                    async: true,
                    processData: false,
                    cache: false,
                    success: function (data) {
                        if (data) {
                            toastr.success('链接已删除成功！', '删除链接');
                            that.closest('tr').remove();
                        } else {
                            toastr.error('链接删除失败！', '删除链接');
                        }
                    },
                    error: function (xhr) {
                        toastr.error('链接删除失败！', '删除链接');
                    }
                });

                e.preventDefault();
            })

        });
    </script>

}